home *** CD-ROM | disk | FTP | other *** search
/ Cracking 1 / Cracking I..iso / Tools / Ostatní / aPLib v0.26b / examples / c / appack.cpp next >
Encoding:
C/C++ Source or Header  |  2001-12-15  |  7.0 KB  |  235 lines

  1. /*
  2.  * aPLib compression library  -  the smaller the better :)
  3.  *
  4.  * C/C++ example (tested with BCC32, DJGPP, Watcom and VC++)
  5.  *
  6.  * Copyright (c) 1998-2000 by Joergen Ibsen / Jibz
  7.  * All Rights Reserved
  8.  */
  9.  
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <io.h>
  13. #include <conio.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include <errno.h>
  17.  
  18. // Compiler specific stuff
  19. #if defined DJGPP
  20.  
  21. #include <file.h>
  22. #include "..\..\lib\djgpp\aplib.h"
  23. #define __cdecl
  24.  
  25. #elif defined VC
  26.  
  27. #include "..\..\lib\vc\aplib.h"
  28.  
  29. #elif defined DLL
  30.  
  31. #include "..\..\lib\dll\aplib.h"
  32.  
  33. #else // assume WATCOM
  34.  
  35. #include "..\..\lib\watcom\aplib.h"
  36.  
  37. #endif
  38.  
  39. // Possible errors
  40. enum { OPEN_ERR = 1, READ_ERR,  SMALL_ERR,  TMP_ERR,
  41.        PACK_ERR,     MEM_ERR,   WRITE_ERR };
  42.  
  43. // Program name and version
  44. char *versionstr = "aPLib-Pack";
  45.  
  46. // Global variables used
  47. int infile = -1, outfile = -1;
  48. unsigned int infilesize, outfilesize;
  49. unsigned char *outbuffer = NULL, *inbuffer = NULL;
  50. unsigned char *testbuffer = NULL, *workbuffer = NULL;
  51.  
  52. unsigned char rotator[] = "-\\|/";
  53. int rotatorpos = 0;
  54.  
  55. // =========================================================================
  56. //  MISC. FUNCTIONS
  57. // =========================================================================
  58.  
  59. // Some I/O error handler :)
  60. void myerror(int n)
  61. {
  62.    // Print out error information
  63.    printf("\n\n\007ERR: ");
  64.    switch (n)
  65.    {
  66.       case OPEN_ERR  : printf("Unable to open input-file!\n"); break;
  67.       case PACK_ERR  : printf("An error occured while packing!\n"); break;
  68.       case READ_ERR  : printf("Unable to read from input-file!\n"); break;
  69.       case TMP_ERR   : printf("Unable to create 'APACKTMP.$$$'!\n"); break;
  70.       case SMALL_ERR : printf("File is too small to pack!\n"); break;
  71.       case MEM_ERR   : printf("Not enough memory!\n"); break;
  72.       case WRITE_ERR : printf("Unable to write to output-file!\n"); break;
  73.       default        : printf("An unknown error occured!\n");
  74.    }
  75.  
  76.    // Free memory and close files
  77.    if (outbuffer != NULL) free(outbuffer);
  78.    if (inbuffer != NULL) free(inbuffer);
  79.    if (testbuffer != NULL) free(testbuffer);
  80.    if (workbuffer != NULL) free(workbuffer);
  81.    if (infile != -1) close(infile);
  82.    if (outfile != -1) close(outfile);
  83.    remove("APACKTMP.$$$");
  84.  
  85.    // Exit with errorlevel = error no.
  86.    exit(n);
  87. }
  88.  
  89. void syntax()
  90. {
  91.    printf("  Syntax:   aPPack <input file> [output file]\n\n");
  92. }
  93.  
  94. int __cdecl callback(unsigned int inpos, unsigned int outpos)
  95. {
  96.    unsigned char ch;
  97.    printf("\r%c Packing data                 -> %3d%%", rotator[rotatorpos], inpos * 100 / infilesize);
  98.    rotatorpos = (rotatorpos + 1) & 0x0003;
  99.  
  100.    // Check for ESC-hit
  101.    if (kbhit())
  102.    {
  103.       ch = getch();
  104.       if (ch == 0) ch = getch();
  105.       if (ch == 27)
  106.       {
  107.          return (0); // abort packing
  108.       }
  109.    }
  110.    return (1); // continue packing
  111. }
  112.  
  113. // =========================================================================
  114. //  MAIN
  115. // =========================================================================
  116.  
  117. int main(int argc, char *argv[])
  118. {
  119.    int infilearg = 0, outfilearg = 0;
  120.  
  121.    unsigned int packedlength, an_error;
  122.  
  123.    int i;
  124.    unsigned int j;
  125.  
  126.    // Write name and copyright notice
  127.    printf("===============================================================================\n");
  128.    printf("%-25s       Copyright (c) 1998-2000 by Joergen Ibsen / Jibz\n", versionstr);
  129.    printf("                                                            All Rights Reserved\n");
  130.    printf("===============================================================================\n\n");
  131.  
  132.    // Write syntax when not enough parameters
  133.    if (argc < 2)
  134.    {
  135.       syntax();
  136.       return(1);
  137.    }
  138.  
  139.    // Parse command line
  140.    for (i = 1; i < argc; i++)
  141.    {
  142.       if (infilearg != 0)
  143.       {
  144.          if (outfilearg == 0) outfilearg = i;
  145.       } else infilearg = i;
  146.    }
  147.  
  148.    // Print syntax if no in-file
  149.    if (infilearg == 0)
  150.    {
  151.       syntax();
  152.       return(1);
  153.    }
  154.  
  155.    // Open input file
  156.    printf("■ Opening files\n");
  157.    if ((infile = open(argv[infilearg], O_RDONLY | O_BINARY)) == -1) myerror(OPEN_ERR);
  158.  
  159.    // Get file size and check it
  160.    lseek(infile, 0, SEEK_SET);
  161.    infilesize = filelength(infile);
  162.    if (infilesize < 64) myerror(SMALL_ERR);
  163.  
  164.    printf("   - Data size    : %u bytes\n", infilesize);
  165.  
  166.    // Create tmp file
  167.    if ((outfile = open("APACKTMP.$$$", O_WRONLY | O_CREAT | O_BINARY | O_TRUNC, S_IREAD | S_IWRITE)) == -1) myerror(TMP_ERR);
  168.  
  169.    printf("■ Allocating memory\n");
  170.    // Allocate memory for compression buffers
  171.    if ((inbuffer = (unsigned char *) malloc(infilesize)) == NULL) myerror(MEM_ERR);
  172.    if ((testbuffer = (unsigned char *) malloc(infilesize)) == NULL) myerror(MEM_ERR);
  173.    if ((outbuffer = (unsigned char *) malloc(((infilesize*9)/8)+16)) == NULL) myerror(MEM_ERR);
  174.  
  175.    // Allocate work mem for the packer
  176.    if ((workbuffer = (unsigned char *) malloc(aP_workmem_size(infilesize))) == NULL) myerror(MEM_ERR);
  177.  
  178.    // Read data into memory
  179.    read(infile, inbuffer, infilesize);
  180.  
  181.    // Pack data
  182.    if ((packedlength = aP_pack(inbuffer, outbuffer, infilesize, workbuffer, callback)) == 0) myerror(PACK_ERR);
  183.  
  184.    // Print out the 100% line :)
  185.    printf("\r■ Packing data                 -> %u bytes\n", packedlength);
  186.  
  187.    // Write code to output file
  188.    lseek(outfile, 0, SEEK_SET);
  189.    write(outfile, outbuffer, packedlength);
  190.    outfilesize = filelength(outfile);
  191.  
  192.    // Depack compressed data and compare to original
  193.    printf("■ Depacking and comparing      -> ");
  194.    printf("Depacked %u bytes - ", aP_depack_asm(outbuffer, testbuffer));
  195.  
  196.    an_error = 0;
  197.    for (j = 0; j < infilesize; j++) if (testbuffer[j] != inbuffer[j]) an_error = 1;
  198.  
  199.    if (an_error) printf("There were errors!\n"); else printf("Ok.\n");
  200.  
  201.    // Free memory for compression buffers
  202.    printf("■ Freeing memory\n");
  203.    if (outbuffer != NULL) free(outbuffer);
  204.    if (inbuffer != NULL) free(inbuffer);
  205.    if (testbuffer != NULL) free(testbuffer);
  206.    if (workbuffer != NULL) free(workbuffer);
  207.  
  208.    // Close in and out files
  209.    printf("■ Closing files\n");
  210.    if (infile != -1) close(infile);
  211.    if (outfile != -1) close(outfile);
  212.  
  213.    // Rename the output file into the right name
  214.    if (outfilearg != 0)
  215.    {
  216.       if (remove(argv[outfilearg]) != 0) if (errno == EACCES) myerror(WRITE_ERR);
  217.       if (rename("APACKTMP.$$$", argv[outfilearg]) != 0) myerror(WRITE_ERR);
  218.    } else {
  219.       printf("   - No output file specified ... writing to 'OUT.DAT'\n");
  220.       if (remove("OUT.DAT") != 0) if (errno == EACCES) myerror(WRITE_ERR);
  221.       if (rename("APACKTMP.$$$", "OUT.DAT") != 0) myerror(WRITE_ERR);
  222.    }
  223.  
  224.    // Print out results
  225.    printf("\nDone ... compression ratio: %d%% ", 100 - ((outfilesize * 100) / infilesize));
  226.    printf("(%u bytes -> %u bytes)\n", infilesize, outfilesize);
  227.  
  228.    // We're happy :)
  229.    return (0);
  230. }
  231.  
  232. // =========================================================================
  233. //  END
  234. // =========================================================================
  235.